package com.acwing.partition2;

import java.io.*;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

/**
 * @author `RKC`
 * @date 2021/12/28 16:44
 */
public class AC175电路维修 {

    private static final int R = 510, C = 510, INF = 0x3f3f3f3f;
    private static int n = 0, m = 0;
    private static char[][] graph = new char[R][C];
    private static int[][] dist = new int[R][C], pointDirs = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}}, graphDirs = {{-1, -1}, {-1, 0}, {0, 0}, {0, -1}};
    private static boolean[][] visited = new boolean[R][C];
    private static char[] dirs = {'\\', '/', '\\', '/'};

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split("\\s+");
        int k = Integer.parseInt(s[0]);
        while (k-- > 0) {
            s = reader.readLine().split("\\s+");
            n = Integer.parseInt(s[0]);
            m = Integer.parseInt(s[1]);
            for (int i = 0; i < n; i++) {
                String str = reader.readLine();
                for (int j = 0; j < m; j++) {
                    graph[i][j] = str.charAt(j);
                }
            }
            if (((n + m) & 1) == 1) writer.write("NO SOLUTION\n");
            else writer.write(bfs() + "\n");
        }
        writer.flush();
    }

    private static int bfs() {
        for (int i = 0; i <= n; i++) Arrays.fill(visited[i], false);
        for (int i = 0; i <= n; i++) Arrays.fill(dist[i], INF);
        dist[0][0] = 0;
        Deque<int[]> queue = new LinkedList<>();
        queue.addLast(new int[]{0, 0});
        //做一遍特殊的dijkstra，权值0就添加到队头，权值1就添加到队尾
        while (!queue.isEmpty()) {
            int[] curr = queue.pollFirst();
            int currX = curr[0], currY = curr[1];
            if (currX == n && currY == m) return dist[currX][currY];
            if (visited[currX][currY]) continue;
            visited[currX][currY] = true;
            //当前位置为(0,0)点沿着 (pointDirs[i][0], pointDirs[i][1]) 沿着理想的方向 dirs[i] 走 (graphDirs[i][0], graphDirs[i][1]) 到下一个节点
            for (int i = 0; i < pointDirs.length; i++) {
                int nextX = pointDirs[i][0] + currX, nextY = pointDirs[i][1] + currY;
                if (nextX < 0 || nextX > n || nextY < 0 || nextY > m) continue;
                //这里的x和y才表示的是存储图的对应的位置并检查是否是理想方向，如果不是就需要转动，权值加1
                int x = graphDirs[i][0] + currX, y = graphDirs[i][1] + currY, w = graph[x][y] == dirs[i] ? 0 : 1;
                int dis = dist[currX][currY] + w;
                if (dis < dist[nextX][nextY]) {
                    dist[nextX][nextY] = dis;
                    //根据权值指定添加到队列中的方向，特殊的dijkstra
                    if (w == 1) queue.addLast(new int[]{nextX, nextY});
                    else queue.addFirst(new int[]{nextX, nextY});
                }
            }
        }
        return -1;
    }
}
